<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="用户ID" prop="userId">
        <el-input
          v-model="queryParams.userId"
          placeholder="请输入用户ID"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="转正日期" prop="formalDate">
        <el-date-picker clearable
          v-model="queryParams.formalDate"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择转正日期">
        </el-date-picker>
      </el-form-item>
      <!-- <el-form-item label="审核人" prop="auditBy">
        <el-input
          v-model="queryParams.auditBy"
          placeholder="请输入审核人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <el-form-item label="审核时间" prop="auditTime">
        <el-date-picker clearable
          v-model="queryParams.auditTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择审核时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <!-- <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:formal:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:formal:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:formal:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:formal:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row> -->

    <el-table v-loading="loading" :data="formalList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="正式党员ID" align="center" prop="formalId" />
      <el-table-column label="用户ID" align="center" prop="userId" />
      <el-table-column label="用户姓名" align="center" prop="nickName" />
      <el-table-column label="转正申请书" align="center" prop="applicationFile">
        <template slot-scope="scope">
          <a v-if="scope.row.applicationFile" 
             :href="scope.row.applicationFile" 
             download
             style="text-decoration: underline; color: #409EFF; cursor: pointer;">
            {{ getFileName(scope.row.applicationFile) }}
          </a>
          <span v-else>-</span>
        </template>
      </el-table-column>
      <el-table-column label="预备期总结" align="center" prop="summaryReport">
        <template slot-scope="scope">
          <a v-if="scope.row.summaryReport" 
             :href="scope.row.summaryReport" 
             download
             style="text-decoration: underline; color: #409EFF; cursor: pointer;">
            {{ getFileName(scope.row.summaryReport) }}
          </a>
          <span v-else>-</span>
        </template>
      </el-table-column>
      <el-table-column label="支部意见" align="center" prop="branchOpinion">
        <template slot-scope="scope">
          <a v-if="scope.row.branchOpinion" 
             :href="scope.row.branchOpinion" 
             download
             style="text-decoration: underline; color: #409EFF; cursor: pointer;">
            {{ getFileName(scope.row.branchOpinion) }}
          </a>
          <span v-else>-</span>
        </template>
      </el-table-column>
      <el-table-column label="批准文件" align="center" prop="approvalFile">
        <template slot-scope="scope">
          <a v-if="scope.row.approvalFile" 
             :href="scope.row.approvalFile" 
             download
             style="text-decoration: underline; color: #409EFF; cursor: pointer;">
            {{ getFileName(scope.row.approvalFile) }}
          </a>
          <span v-else>-</span>
        </template>
      </el-table-column>
      <el-table-column label="转正日期" align="center" prop="formalDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.formalDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="审核状态" align="center" prop="auditStatus">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.audit_status" :value="scope.row.auditStatus"/>
        </template>
      </el-table-column>
      <el-table-column label="审核意见" align="center" prop="auditOpinion" show-overflow-tooltip />
      <!-- <el-table-column label="审核人" align="center" prop="auditBy" /> -->
      <el-table-column label="审核时间" align="center" prop="auditTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.auditTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="200">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handleDetail(scope.row)"
          >详情</el-button>
          <el-button
            v-if="scope.row.auditStatus == 0 || scope.row.auditStatus == 2"
            size="mini"
            type="text"
            icon="el-icon-check"
            @click="handleAudit(scope.row)"
            v-hasPermi="['system:formal:audit']"
          >审核</el-button>
          <!-- <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:formal:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:formal:remove']"
          >删除</el-button> -->
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改正式党员转正对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="用户ID" prop="userId">
          <el-input v-model="form.userId" placeholder="请输入用户ID" />
        </el-form-item>
        <el-form-item label="转正申请书" prop="applicationFile">
          <file-upload v-model="form.applicationFile"/>
        </el-form-item>
        <el-form-item label="预备期总结" prop="summaryReport">
          <el-input v-model="form.summaryReport" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="支部意见" prop="branchOpinion">
          <el-input v-model="form.branchOpinion" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="批准文件" prop="approvalFile">
          <file-upload v-model="form.approvalFile"/>
        </el-form-item>
        <el-form-item label="转正日期" prop="formalDate">
          <el-date-picker clearable
            v-model="form.formalDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择转正日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="审核意见" prop="auditOpinion">
          <el-input v-model="form.auditOpinion" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <!-- <el-form-item label="审核人" prop="auditBy">
          <el-input v-model="form.auditBy" placeholder="请输入审核人" />
        </el-form-item> -->
        <el-form-item label="审核时间" prop="auditTime">
          <el-date-picker clearable
            v-model="form.auditTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择审核时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="删除标志" prop="delFlag">
          <el-input v-model="form.delFlag" placeholder="请输入删除标志" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 审核对话框 -->
    <el-dialog title="审核" :visible.sync="auditOpen" width="500px" append-to-body>
      <el-form ref="auditForm" :model="auditForm" :rules="auditRules" label-width="100px">
        <el-form-item label="审核意见" prop="auditOpinion">
          <el-input v-model="auditForm.auditOpinion" type="textarea" :rows="4" placeholder="请输入审核意见" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="warning" @click="submitAudit(2)">需修改</el-button>
        <el-button type="success" @click="submitAudit(1)">通过</el-button>
        <el-button @click="cancelAudit">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 详情对话框 -->
    <el-dialog title="正式党员详情" :visible.sync="detailOpen" width="800px" append-to-body>
      <el-descriptions :column="2" border>
        <el-descriptions-item label="申请人">{{ detailData.nickName || '-' }}</el-descriptions-item>
        <el-descriptions-item label="用户ID">{{ detailData.userId || '-' }}</el-descriptions-item>
        <el-descriptions-item label="申请时间">{{ parseTime(detailData.createTime, '{y}-{m}-{d}') }}</el-descriptions-item>
        <el-descriptions-item label="转正日期">{{ parseTime(detailData.formalDate, '{y}-{m}-{d}') }}</el-descriptions-item>
        <el-descriptions-item label="转正申请书" :span="2">
          <el-link v-if="detailData.applicationFile" type="primary" :href="detailData.applicationFile" :download="getFileName(detailData.applicationFile)">
            {{ getFileName(detailData.applicationFile) }}
          </el-link>
          <span v-else>-</span>
        </el-descriptions-item>
        <el-descriptions-item label="预备期总结" :span="2">
          <el-link v-if="detailData.summaryReport" type="primary" :href="detailData.summaryReport" :download="getFileName(detailData.summaryReport)">
            {{ getFileName(detailData.summaryReport) }}
          </el-link>
          <span v-else>-</span>
        </el-descriptions-item>
        <el-descriptions-item label="支部意见" :span="2">
          <el-link v-if="detailData.branchOpinion" type="primary" :href="detailData.branchOpinion" :download="getFileName(detailData.branchOpinion)">
            {{ getFileName(detailData.branchOpinion) }}
          </el-link>
          <span v-else>-</span>
        </el-descriptions-item>
        <el-descriptions-item label="批准文件" :span="2">
          <el-link v-if="detailData.approvalFile" type="primary" :href="detailData.approvalFile" :download="getFileName(detailData.approvalFile)">
            {{ getFileName(detailData.approvalFile) }}
          </el-link>
          <span v-else>-</span>
        </el-descriptions-item>
        <!-- <el-descriptions-item label="转正决议" :span="2">
          {{ detailData.formalResolution || '-' }}
        </el-descriptions-item> -->
        <el-descriptions-item label="审核状态">
          <dict-tag :options="dict.type.audit_status" :value="detailData.auditStatus"/>
        </el-descriptions-item>
        <el-descriptions-item label="审核时间">{{ parseTime(detailData.auditTime, '{y}-{m}-{d}') }}</el-descriptions-item>
        <el-descriptions-item label="审核意见" :span="2">{{ detailData.auditOpinion || '-' }}</el-descriptions-item>
        <el-descriptions-item label="备注" :span="2">{{ detailData.remark || '-' }}</el-descriptions-item>
      </el-descriptions>
      <div slot="footer" class="dialog-footer">
        <el-button @click="detailOpen = false">关 闭</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listFormal, getFormal, delFormal, addFormal, updateFormal, auditFormal } from "@/api/system/formal"

export default {
  name: "Formal",
  dicts: ['audit_status'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 正式党员转正表格数据
      formalList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 是否显示详情弹出层
      detailOpen: false,
      // 详情数据
      detailData: {},
      // 是否显示审核弹出层
      auditOpen: false,
      // 审核表单
      auditForm: {
        formalId: null,
        auditOpinion: ''
      },
      // 审核表单校验
      auditRules: {
        auditOpinion: [
          { required: true, message: "审核意见不能为空", trigger: "blur" }
        ]
      },
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        userId: null,
        applicationFile: null,
        summaryReport: null,
        branchOpinion: null,
        approvalFile: null,
        formalDate: null,
        auditStatus: null,
        auditOpinion: null,
        auditBy: null,
        auditTime: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        userId: [
          { required: true, message: "用户ID不能为空", trigger: "blur" }
        ],
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    /** 查询正式党员转正列表 */
    getList() {
      this.loading = true
      listFormal(this.queryParams).then(response => {
        this.formalList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        formalId: null,
        userId: null,
        applicationFile: null,
        summaryReport: null,
        branchOpinion: null,
        approvalFile: null,
        formalDate: null,
        auditStatus: null,
        auditOpinion: null,
        auditBy: null,
        auditTime: null,
        delFlag: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null
      }
      this.resetForm("form")
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm")
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.formalId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = "添加正式党员转正"
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const formalId = row.formalId || this.ids
      getFormal(formalId).then(response => {
        this.form = response.data
        this.open = true
        this.title = "修改正式党员转正"
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.formalId != null) {
            updateFormal(this.form).then(response => {
              this.$modal.msgSuccess("修改成功")
              this.open = false
              this.getList()
            })
          } else {
            addFormal(this.form).then(response => {
              this.$modal.msgSuccess("新增成功")
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const formalIds = row.formalId || this.ids
      this.$modal.confirm('是否确认删除正式党员转正编号为"' + formalIds + '"的数据项？').then(function() {
        return delFormal(formalIds)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess("删除成功")
      }).catch(() => {})
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/formal/export', {
        ...this.queryParams
      }, `formal_${new Date().getTime()}.xlsx`)
    },
    /** 详情按钮操作 */
    handleDetail(row) {
      this.detailData = { ...row }
      this.detailOpen = true
    },
    /** 审核按钮操作 */
    handleAudit(row) {
      this.auditForm = {
        formalId: row.formalId,
        auditOpinion: ''
      }
      this.auditOpen = true
    },
    /** 提交审核 */
    submitAudit(status) {
      this.$refs["auditForm"].validate(valid => {
        if (valid) {
          // 设置审核状态和审核时间
          this.auditForm.auditStatus = status
          this.auditForm.auditTime = this.parseTime(new Date(), '{y}-{m}-{d} {h}:{i}:{s}')
          
          auditFormal(this.auditForm).then(response => {
            this.$modal.msgSuccess("审核成功")
            this.auditOpen = false
            this.getList()
          })
        }
      })
    },
    /** 取消审核 */
    cancelAudit() {
      this.auditOpen = false
      this.auditForm = {
        formalId: null,
        auditOpinion: ''
      }
      this.resetForm("auditForm")
    },
    /** 获取文件名 */
    getFileName(fileUrl) {
      if (!fileUrl) return '-'
      const urlParts = fileUrl.split('/')
      return urlParts[urlParts.length - 1]
    }
  }
}
</script>
